package com;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;


public class GameWindows2 extends JPanel implements Runnable, KeyListener {
    private Image  BK= new ImageIcon("bk.jpg").getImage();
    private Image  Down1 = new ImageIcon("npc1down.png").getImage();
    private Image  Down2 = new ImageIcon("npc2down.png").getImage();
    private Image  Up1 = new ImageIcon("npc1up.png").getImage();
    private Image  Up2 = new ImageIcon("npc2up.png").getImage();
    private Image  Left1 = new ImageIcon("npc1lift.png").getImage();
    private Image  Left2 = new ImageIcon("npc2lift.png").getImage();
    private Image  Right1 = new ImageIcon("npc1right.png").getImage();
    private Image  Right2 = new ImageIcon("npc2right.png").getImage();
    private Image Box1 = new ImageIcon("box1.jpg").getImage();
    private Image Boss1 = new ImageIcon("boss2.png").getImage();
    private Image Door = new ImageIcon("door.jpg").getImage();
    private Image de = new ImageIcon("npcde.png").getImage();

    private List<Monster> GuaiShou = new ArrayList<>();

    private int x=0;//角色的x轴坐标
    private int y=0;//角色的y轴坐标
    private String direction="D";//任务方向
    private Boolean isField=false;//让人物运动
    private int m=1;//两张图片轮播
    private int Boxx;//盒子的x轴
    private int Boxy;//盒子的y轴
    private int report=3;//分数
    private Boolean isStart=true;//游戏开始或结束
    private  Boolean isBegin=false;
    private int Doorx=(int)(Math.random()*1050);//们的x轴
    private int Doory=(int)(Math.random()*700);
    GameWindows2(){
        Boxy=(int)(Math.random()*700);
        Boxx=(int)(Math.random()*1050);
        for(int i=0;i<30;i++)
        {
            Monster monster = new Monster();
            monster.x=(int)(Math.random()*1050);//怪兽的x轴
            monster.y=(int)(Math.random()*700);//怪兽的y轴
            GuaiShou.add(monster);
        }
        JFrame jf = new JFrame();
        jf.setTitle("第二层");
        jf.setVisible(true);
        jf.add(this);
        jf.setFocusable(true);
        jf.addKeyListener(this);
        jf.setBounds(200,50,1100,750);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

    @Override
    public void paint(Graphics g) {

        this.setBackground(Color.blue);
        g.drawImage(BK,0,0,1100,750,null);
        g.setFont(new Font("楷体",Font.BOLD,20));
        g.drawString("宝箱数:"+report+"",900,20);//把分数画出来
        g.drawString("目标宝箱数:"+"10"+"",900,40);
        if(direction.equals("R")&&m==0)
        {
            g.drawImage(Right1,x,y,30,30,null);//控制人物的方向
        }
        if(direction.equals("R")&&m==1)
        {
            g.drawImage(Right2,x,y,30,30,null);
        }
        if(direction.equals("L")&&m==0)
        {
            g.drawImage(Left1,x,y,30,30,null);
        }
        if(direction.equals("L")&&m==1)
        {
            g.drawImage(Left2,x,y,30,30,null);
        }
        if(direction.equals("U")&&m==0)
        {
            g.drawImage(Up1,x,y,30,30,null);
        }
        if(direction.equals("U")&&m==1)
        {
            g.drawImage(Up2,x,y,30,30,null);
        }
        if(direction.equals("D")&&m==0)
        {
            g.drawImage(Down1,x,y,30,30,null);
        }
        if(direction.equals("D")&&m==1)
        {
            g.drawImage(Down2,x,y,30,30,null);
        }
        //画箱子
        g.drawImage(Box1,Boxx,Boxy,30,30,null);
        //画怪兽
        for(int i=0;i<GuaiShou.size();i++)
        {
            if(y<GuaiShou.get(i).y+30&&y>GuaiShou.get(i).y-30&&x>GuaiShou.get(i).x-30&&x<GuaiShou.get(i).x+30)
            {
                isStart=false;
                g.drawImage(Boss1,GuaiShou.get(i).x,GuaiShou.get(i).y,30,30,null);
                SmallGame wins = new SmallGame("战斗中...");
                GuaiShou.remove(i);
            }
        }
        //判断游戏结束
        if(report<0)
        {
            isBegin=false;
            new Gameover();
            g.drawImage(BK,x,y,30,30,null);
            g.drawImage(de,x,y,33,25,null);

        }
        if(report>=10)//当达到多少分数时 们开启
        {
            g.drawImage(Door,Doorx,Doory,30,30,null);
            if(y<Doory+30&&y>Doory-30&&x>Doorx-30&&x<Doorx+30)//判断人物是否到达们的旁边
            {    setVisible(false);
               GameWindows3 win2 = new GameWindows3();
                Thread thread=new Thread(win2);
                thread.start();


            }
        }



    }

    @Override
    public void run() {
        while(true)
        {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(isStart&&isBegin)
            {
                //人物移动
                if( direction.equals("R")&&isField&&x<1060)
                {
                    x+=5;
                }
                if(direction.equals("L")&&isField&&x>0)
                {
                    x-=5;
                }
                if (direction.equals("U")&&isField&&y>0)
                {
                    y-=5;
                }
                if(direction.equals("D")&&isField&&y<680)
                {
                    y+=5;
                }
                if(m==1)
                {
                    m=0;
                }
                else
                {
                    m=1;
                }
                // 捡宝箱
                if(y<Boxy+30&&y>Boxy-30&&x>Boxx-30&&x<Boxx+30)
                {
                    Boxx=(int)(Math.random()*1050);
                    Boxy=(int)(Math.random()*700);
                    report++;
                }
                //添加怪兽

                repaint();
            }

        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode())
        {
            case KeyEvent.VK_RIGHT: direction="R";
                isField= true;
                isStart=true;
                isBegin=true;
                SmallGame wins = new SmallGame();
                report+=wins.report1;
                wins.report1=0;
                break;
            case KeyEvent.VK_LEFT: direction="L";
                isField= true;
                isStart=true;
                isBegin=true;
                SmallGame wins1 = new SmallGame();
                report+=wins1.report1;
                wins1.report1=0;
                break;
            case KeyEvent.VK_UP: direction="U";
                isField= true;
                isStart=true;
                isBegin=true;
                SmallGame wins2 = new SmallGame();
                report+=wins2.report1;
                wins2.report1=0;
                break;
            case KeyEvent.VK_DOWN: direction="D";
                isField= true;
                isStart=true;
                isBegin=true;
                SmallGame wins3 = new SmallGame();
                report+=wins3.report1;
                wins3.report1=0;
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode())
        {
            case KeyEvent.VK_RIGHT:isField=false; isBegin=false;break;
            case KeyEvent.VK_LEFT: isField=false;isBegin=false;break;
            case KeyEvent.VK_UP: isField=false;isBegin=false;break;
            case KeyEvent.VK_DOWN: isField=false;isBegin=false;break;
        }
    }
}
